package com.xintonghua.bussiness.ui.fragment.client.search;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.model.ConflictAlgorithm;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.MyAppLication;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.SearchAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.database.SearchHistoryModel;
import com.xintonghua.bussiness.util.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wanxianyi
 * 时间：2018/4/1.
 * 包名：com.xintonghua.bussiness.ui.fragment.client.search
 */

public class SearchActivity extends BaseActivity {
    @BindView(R.id.edt_search)
    EditText edtSearch;
    @BindView(R.id.tv_close)
    TextView tvClose;
    @BindView(R.id.tv_clear)
    TextView tvClear;
    @BindView(R.id.lv_search_history)
    ListView lvSearchHistory;
    private List<SearchHistoryModel> histories = new ArrayList<>();
    private String keyword;
    SearchAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        adapter = new SearchAdapter(histories, this);
        lvSearchHistory.setAdapter(adapter);
        getSearchHistoryByWhere();
        edtSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    keyword = edtSearch.getText().toString();
                    if (TextUtils.isEmpty(keyword)) {
                        return false;
                    }
                    MyUtils.hideSoftInput(SearchActivity.this);
                    addSearchHistory(keyword);
                    Bundle bundle = new Bundle();
                    bundle.putString(BuildConfig.FLAVOR, keyword);
                    openActivity(SearchResultActivity.class, bundle);
                    return true;
                }
                return false;
            }
        });
        lvSearchHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (adapter == null) {
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString(BuildConfig.FLAVOR, adapter.getItem(position).getKeyword());
                openActivity(SearchResultActivity.class, bundle);
            }
        });
    }

    /**
     * 获取搜索历史
     */
    private void getSearchHistoryByWhere() {
        histories.clear();
        histories.addAll(MyAppLication.sDb.query(new QueryBuilder<>(SearchHistoryModel.class)));
        adapter.notifyDataSetChanged();
    }

    /**
     * 添加搜索历史
     */
    private void addSearchHistory(String keyword) {
        SearchHistoryModel model = new SearchHistoryModel(keyword.hashCode(), keyword);
        long result = MyAppLication.sDb.insert(model, ConflictAlgorithm.Abort);
        if (result != -1) {
            adapter.add(model);
        }
    }

    /**
     * 清除所有搜索历史
     */
    private void clearSearchHistory() {
        int result = MyAppLication.sDb.deleteAll(SearchHistoryModel.class);
        if (result > 0) {
            adapter.clean();
        }
    }


    @OnClick({R.id.tv_clear, R.id.tv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_clear:
                clearSearchHistory();
                break;
            case R.id.tv_close:
                finish();
                break;
        }
    }
}
